package com.heima.admin.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.entity.AdSensitive;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.admin.dtos.SensitiveDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@Api(tags = "敏感词设置",description = "敏感词管理API")
public class SensitiveController {
    @Autowired
    AdSensitiveService adSensitiveService;

    /**
     * 分页查询敏感词
     * @param dto
     * @return
     */
        @ApiOperation(value = "根据名称分页查询敏感词")
        @PostMapping("api/v1/sensitive/list")
        public PageResult<AdSensitive> list(@RequestBody SensitiveDto dto){
            return adSensitiveService.findByPage(dto);
        }

    /**
     * 添加敏感词
      * @RequestBody SensitiveDto
     */
        @ApiOperation(value = "添加敏感词语句")
        @PostMapping("api/v1/sensitive/save")
        public ResponseResult saveSensitives(@RequestBody SensitiveDto dto){
            AdSensitive adSensitive = new AdSensitive();
            //复制数据
            BeanUtils.copyProperties(dto,adSensitive);
            //设置创建时间
            adSensitive.setCreatedTime(new Date());
            //如果添加信息为空，抛出异常
            if(dto.getSensitives().isEmpty()){
                return ResponseResult.error(AppHttpCodeEnum.SAVE_FAILED);

            }
            adSensitiveService.save(adSensitive);
            return ResponseResult.ok();
        }

    /**
     * 修改敏感词
      * @param dto
     * @return
     */
    @ApiOperation(value = "修改敏感词语句")
    @PutMapping("api/v1/sensitive/update")
        public ResponseResult updateSensitives(@RequestBody SensitiveDto dto){
            AdSensitive adSensitive = new AdSensitive();
            //复制请求数据
            BeanUtils.copyProperties(dto,adSensitive);
            adSensitive.setCreatedTime(new Date());
            //如果id为空，信息不存在
            if(dto.getId()==null){
                return ResponseResult.error(AppHttpCodeEnum.DATA_NOT_EXIST);

            }
            LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdSensitive::getId,dto.getId());
            //如果修改信息为空，抛出异常
            if (dto.getSensitives().isEmpty()){
                return ResponseResult.error(AppHttpCodeEnum.UPDATE_FAILED);

            }
            adSensitiveService.update(adSensitive,queryWrapper);
            return ResponseResult.ok();
        }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @ApiOperation(value = "删除敏感词语句")
    @DeleteMapping("api/v1/sensitive/del/{id}")
        public ResponseResult deleteSensitives(@PathVariable("id") Integer id){
            if (id==null){
                return ResponseResult.error(AppHttpCodeEnum.DATA_NOT_EXIST);

            }
            adSensitiveService.removeById(id);
            return ResponseResult.ok();
        }
}
